package com.cet.designapplication.decorator.strategy;

import io.netty.channel.ChannelOutboundBuffer;
import org.bouncycastle.util.encoders.UTF8;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * @author NSOC
 */

@Component
public class DelayScoreAndRedBagSendHandler implements ScoreAndRegBagSendStrategy{


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${delay.service.time}")
    private String delayServiceTime;

    @Autowired
    private MessagePostProcessor messagePostProcessor;

    @Override
    public int getServiceLevel() {
        return 1;
    }

    @Override
    public void sendScoreAndRegBag(String productId, BigDecimal price) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        Map<String, Object> messageMap = new HashMap<>(8);
        messageMap.put("productId", productId);
        messageMap.put("price", price);
        rabbitTemplate.convertAndSend("normalExchange", "myRKey", messageMap, (message) -> {
            message.getMessageProperties().setContentEncoding("UTF-8");
            message.getMessageProperties().setExpiration(delayServiceTime);
            return message;
        }, correlationData);
        System.out.println("延迟处理，时间 = " + delayServiceTime);
    }
}
